//
// Created by Administrator on 2021/7/2.
//

// N 叉数的层序遍历
#include <utility>
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

// Definition for a Node.
class Node {
public:
    int val;
    vector<Node *> children;

    Node() { val = 0; }

    explicit Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node *> _children) {
        val = _val;
        children = std::move(_children);
    }
};


class Solution {
public:
    vector<vector<int>> levelOrder(Node *root) {
        vector<vector<int>>ans;
        if (root == nullptr) return ans;
        queue<Node*>q;
        q.push(root);
        while (!q.empty()){
           auto levelSize = q.size();
           ans.emplace_back();
           for(int i =0;i<levelSize;++i){
               auto p = q.front();
               q.pop();
               ans.back().push_back(p->val);
               for(auto &x:p->children){
                   if (x!= nullptr) q.push(x);
               }
           }
        }
        return ans;
    }
};

int main() {
    auto n1 = Node(1), n2 = Node(3), n3 = Node(2), n4 = Node(4), n5 = Node(5), n6 = Node(6);
    n1.children = std::move(vector<Node *>{&n2, &n3, &n4});
    n2.children = std::move(vector<Node *>{&n5, &n6});
    Solution sol;
    auto ans = sol.levelOrder(&n1);
    for(auto &level:ans){
        for(auto &x:level){
            cout << x ;
            cout << " ";
        }
        cout << endl;
    }
    return 0;
}